﻿using Blm.Utils.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Sys;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.Sys;
using VisionCloud.Domain.Extension;
using VisionCloud.Domain.Repository.Interface.Sys;

namespace VisionCloud.Domain.Repository.Implement.Sys
{
    public class SysMenuRepository : BaseRepository<SysMenu>, ISysMenuRepository
    {
        public async Task<SysMenuDto> FindSysMenu(SysMenuDto sysMenuDto)
        {
            var result = Db.SysMenus.Where(p => p.Name == sysMenuDto.Name)
                    .Select(w => new SysMenuDto()
                    {
                        MenuId = w.MenuId,
                        Name = w.Name,
                        Description = w.Description,
                        Url = w.Url,
                        Icon = w.Icon,
                        ParentId = w.ParentId,
                        Level = w.Level,
                        Component = w.Component,
                        Sort = w.Sort
                    }).SingleOrDefault();

            return result;
        }

        public async Task<List<VMenu>> GetMenuList()
        {
            //VMenu root = new VMenu() { MenuId = Guid.Empty, Name = "Root" };
            List<VMenu> list = new List<VMenu>();
            string sql = "select * from vw_menu_tree";
            using (var ds = Db.GetDataSet(System.Data.CommandType.Text, sql))
            {
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
                    {
                        VMenu menu = new VMenu();
                        menu.MenuId = dr["MenuId"].ToGuid();
                        menu.Name = dr["Name"].NullToString();
                        menu.ParentId = dr["ParentId"].ToGuid();
                        menu.ParentName = dr["MenuId"].NullToString();
                        menu.Level = dr["Level"].ToInt32();
                        menu.Sort = dr["Sort"].ToInt32();
                        menu.Url = dr["Url"].NullToString();
                        menu.Component = dr["Component"].NullToString();

                        list.Add(menu);
                    }
                }
            }
            return list;
        }

        public async Task<ApiPageResult> GetPage(SysMenuCondition condition)
        {
            var list = await Query()
                .WhereIf(!string.IsNullOrWhiteSpace(condition.Name), w => w.Name.Contains(condition.Name))
                .WhereIf(condition.MenuId != Guid.Empty, w => w.MenuId.NullToString().Contains(condition.MenuId.NullToString()))
                .WhereIf(condition.ParentId != Guid.Empty, w => w.ParentId.NullToString().Contains(condition.ParentId.NullToString()))
                .Where(p => p.IsDeleted == false)
                .Select(w => w.CopyTo<SysMenuDto>())
                .DefaultOrderByDescending(condition, w => w.LastUpdatedDate)
                .ToPageList(condition);

            var result = condition.CopyTo<ApiPageResult>();
            result.PageData = list;
            return result;
        }
    }
}